package com.ogawa.jopai.devicemanage.viewmodel

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.ogawa.base.base.BaseViewModel
import com.ogawa.base.bean.LoadState
import com.ogawa.base.constants.Constants.Companion.NET_DEF_PLATFORM
import com.ogawa.projectcommon.utils.dataConvert
import com.ogawa.projectcommon.utils.launch
import com.ogawa.projectcommon.network.RetrofitManager
import com.ogawa.jopai.devicemanage.bean.DeviceTypeBean
import com.ogawa.jopai.devicemanage.service.MassageApiService
import kotlinx.coroutines.cancel

class DeviceSelectViewModel : BaseViewModel() {
    internal val deviceTypeBeans = MutableLiveData<List<DeviceTypeBean>>()

    fun getDeviceTypes() {
        launch({
            loadState.value = LoadState.Loading("")
            deviceTypeBeans.value = RetrofitManager.INSTANCE.createService(MassageApiService::class.java)
                .getDeviceTypes(NET_DEF_PLATFORM)
                .dataConvert()

            loadState.value = LoadState.Success("")
        }
            , { loadState.value = LoadState.Fail(it.message ?: "") }
            , {})
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    open fun ondestory() {
        LogUtils.e("ON_DESTROY")
        viewModelScope.cancel()
    }

}
